package com.chenl.provider.config.Redis;

import com.alibaba.fastjson.JSON;
import java.nio.charset.Charset;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.Assert;

public class ObjectStringRedisSerializer implements RedisSerializer<Object> {
    private final Charset charset;
    private final String target;
    private final String replacement;

    public ObjectStringRedisSerializer() {
        this(Charset.forName("UTF8"));
    }

    public ObjectStringRedisSerializer(Charset charset) {
        this.target = "\"";
        this.replacement = "";
        Assert.notNull(charset, "Charset must not be null!");
        this.charset = charset;
    }

    public String deserialize(byte[] bytes) {
        return bytes == null ? null : new String(bytes, this.charset);
    }

    public byte[] serialize(Object object) {
        String string = JSON.toJSONString(object);
        if (string == null) {
            return null;
        } else {
            string = string.replace("\"", "");
            return string.getBytes(this.charset);
        }
    }
}
